/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.TypeNameRepeatException;
import com.huibur.devicecheck.model.param.TypeParam;
import com.huibur.devicecheck.model.query.TypeQuery;
import com.huibur.devicecheck.model.vo.TypeVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.ITypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*******************************************************
* Title: TypeController.java
* Description:
* @author baiqingshuo
* @date 2021/9/29
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/types")
@Api(value = "类别管理",tags = {"类别管理"})
@Slf4j
public class TypeController {

    @Autowired
    ITypeService iTypeService;

    /**
     * 新增类别
     * @param typeParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增类别",response = ObjRes.class)
    public ObjRes<Void> insertType(@RequestBody TypeParam typeParam) {
        try{
            return iTypeService.insertType(typeParam);
        } catch (TypeNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TYPE_NAME_REPEAT.getCode(),ResultCodeEnum.TYPE_NAME_REPEAT.getLabel());
        }

    }

    /**
     * 删除类别
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除类别",response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id){
        return new ObjRes<Boolean>().data(iTypeService.deleteById(id));
    }

    /**
     * 编辑类别
     * @param id
     * @param typeParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑类别",response = ObjRes.class)
    public ObjRes<Void> updateType(@PathVariable Long id,@RequestBody TypeParam typeParam) {
        try{
            return iTypeService.updateType(id,typeParam);
        } catch (TypeNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes<Void>(ResultCodeEnum.TYPE_NAME_REPEAT.getCode(), ResultCodeEnum.TYPE_NAME_REPEAT.getLabel());
        }

    }

    /**
     * 类别列表(查询并分页)
     * @param typeQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "类别列表(查询并分页)",response = PageRes.class)
    public PageRes<TypeVO> selectListType(TypeQuery typeQuery) {
        return iTypeService.selectListType(typeQuery);
    }

    /**
     * 获取类别详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取类别详情")
    public ObjRes<TypeVO> selectTypeById(@PathVariable Long id) {
        return new ObjRes<TypeVO>().data(iTypeService.selectTypeById(id));
    }

}
